package com.ysd.lis.service.cri.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.cri.CriPost;
import com.ysd.lis.entity.cri.CriPostUser;
import com.ysd.lis.mapper.cri.CriPostMapper;
import com.ysd.lis.mapper.sys.SysUserMapper;
import com.ysd.lis.request.CriPostUserDto;
import com.ysd.lis.service.cri.CriPostService;
import com.ysd.lis.service.cri.CriPostUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-02-10
 */
@Service
public class CriPostServiceImpl extends ServiceImpl<CriPostMapper, CriPost> implements CriPostService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private CriPostUserService sysPostUserService;
    @Override
    public Result findPostList(String keywords) {
        LambdaQueryWrapper<CriPost> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CriPost::getDelFlag,0);
        lambdaQueryWrapper.orderByAsc(CriPost::getSeq);
        if (StrUtil.isNotEmpty(keywords)) {
            lambdaQueryWrapper.and(wrapper ->
                    wrapper.like(CriPost::getPostNo, keywords)
                            .or()
                            .like(CriPost::getPostNa, keywords)
            );
        }
        List<CriPost> sysPosts = this.baseMapper.selectList(lambdaQueryWrapper);
        return Result.succ(sysPosts);
    }

    @Override
    public Result saveOrUpdatePost(CriPost CriPost) {
        boolean save = this.saveOrUpdate(CriPost);
        if (save) {
            return Result.succ(1, "成功！", null);
        } else {
            return Result.fail("失败！");
        }
    }

    @Override
    public Result deletePostList(List<CriPost> CriPostList) {
        List<String> ids = CriPostList.stream().map(CriPost::getId).collect(Collectors.toList());
        this.baseMapper.deleteBatchIds(ids);
        return Result.succ(1, "删除成功！", null);
    }

    @Override
    public Result findUserCompareByPost(String postNo) {
        if (StringUtils.isBlank(postNo)){
            return Result.fail("岗位编号不能为空");
        }
        MPJLambdaWrapper<SysUser> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(SysUser.class);
        wrapper.leftJoin(CriPostUser.class, p -> p.eq(CriPostUser::getUserNo, SysUser::getUsername).eq(CriPostUser::getDelFlag, 0));
        wrapper.eq(CriPostUser::getPostNo, postNo);
        List<SysUser> sysUsers = sysUserMapper.selectJoinList(SysUser.class, wrapper);
        return Result.succ(sysUsers);
    }

    @Override
    public Result saveBatchPostUser(List<CriPostUser> CriPost) {
        boolean b = sysPostUserService.saveBatch(CriPost);
        if (b) {
            return Result.succ(1, "添加成功", null);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result batchDeletePostUserList(CriPostUserDto sysPostUserDto) {
        LambdaQueryWrapper<CriPostUser>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CriPostUser::getPostNo,sysPostUserDto.getPostNo());
        lambdaQueryWrapper.in(CriPostUser::getUserNo,sysPostUserDto.getUserNoList());
        sysPostUserService.remove(lambdaQueryWrapper);
        return Result.succ(1, "删除成功", null);
    }
}
